import 'babel-polyfill'
import Promise from 'es6-promise'
Promise.polyfill()

import Vue from 'vue'
import VueRouter from 'vue-router'
import Vuex from 'vuex'
import VueI18n from 'vue-i18n'
import './processConfig.js'

import App from './App.vue'
import './tools/index.js'
import filters from './filters.js'
import store from './store/index.js'
import Api_SSDP_Mapping from './SSDPConfig.js'
// 单独引入 elementUI的tree组件，迁移项目组大量使用了此组件（同时也单独引入了Table, TableColumn，组织架构页面使用了树形表格）
import { Tree, Table, TableColumn, Input, Select, Button, Option } from "element-ui";
import 'element-ui/lib/theme-chalk/index.css';
// import formCreate from "@form-create/iview4";

import ripples, { configSystemRoute } from 'ripples'
import routeConfig from './router/index.js'
// import 'ripples/dist/styles/ripples.css'

import '@ripples/iview-crcloud-theme' // 华润云风格主题
// import '@ripples/iview-crc-theme' // 集团风格主题
import '@/styles/index.less'

import format from './components/common/Format.vue'
import NameSpace from './components/common/NameSpace.vue'
import InputNumberWithUnit from './components/common/InputNumberWithUnit.vue'
import automatePageTable from './components/common/automatePageTable/automatePageTable.vue'
import moreBtn from './views/nacosList/moreBtn'
// import { CoreModuleName } from "./const.js";
import WujieVue from 'wujie-vue2'
import tripRoute from './router/trip.js'

Vue.component('Format', format)
Vue.component('NameSpace', NameSpace)
Vue.component('InputNumberWithUnit', InputNumberWithUnit)
Vue.component('automatePageTable', automatePageTable)
Vue.component('moreBtn', moreBtn)
Vue.use(VueRouter)
Vue.use(Vuex)
Vue.use(VueI18n)
Vue.use(Tree);
Vue.use(Table);
Vue.use(TableColumn);
Vue.use(Input);
Vue.use(Select);
Vue.use(Button);
Vue.use(Option);
// Vue.use(formCreate);
Vue.use(WujieVue)

Vue.config.devtools = window.RPConfig.devtools
// Vue Dev tools
if (process.env.NODE_ENV === 'development') {
  Vue.config.devtools = true
}

for (let filter of filters) {
  Vue.filter(filter.name, filter.fn)
}

// 自动设置语言
// const navLang = navigator.language
// const localLang = navLang === 'zh-CN' || navLang === 'en-US' ? navLang : false
// const lang = window.localStorage.getItem('language') || localLang || 'zh-CN'
// Vue.config.lang = lang

var routes = []
 routeConfig.system.children.push(...tripRoute.trip)
 console.log('routeConfig.system :>> ', routeConfig.system);
// 路由二次处理：统一添加layout父组件
configSystemRoute(routes, routeConfig.system)
routes.push(routeConfig.addUserInfo)
routes.push(routeConfig.forgotPassword)
routes.push(routeConfig.portal) 
const router = new VueRouter({ routes })

// SSDP 配置
const { openSSDP, ssdp_config } = window.RPConfig
const SSDPConfig = !!openSSDP && ssdp_config && Reflect.ownKeys(ssdp_config).length ? ssdp_config : Api_SSDP_Mapping

/**
 * Ripples Vue插件注册
 *   - 通过router、axios拦截器接入平台统一登录&权限体系
 *   - 全局注册iview4.7基础组件、平台表单组件、业务组件
 *   - 提供$http 、$util、$tool 工具库
 */
Vue.use(ripples, {
  router,
  store,
  Api_SSDP_Mapping: SSDPConfig,
  app: {
    title: '通用开发平台',
    //配置主页路由信息
    home: {
      path: '/home',
      title: '门户页',
      name: 'home_index'
    }
  }
})
// // 忘记密码设置到公共页面白名单
// store.commit(CoreModuleName + "/setPublicPages", "/forgot-password");
// store.commit(CoreModuleName + "/setPublicPages", "/reset-password");

router.beforeEach((to, from, next) => {
  $channel.$emit('RouterChangeBefore')
  $channel.$emit('ActionBarEvent', [])
  next()
})

router.afterEach(() => {
  window.scrollTo(0, 0)
})

window.$channel = window.$channel || new Vue()

const vueOption = {
  el: '#app',
  router,
  store,
  render: h => h(App),
  data: {
    routeChangeTasks: [],
    currentPageName: ''
  },
  mounted() {
    this.currentPageName = this.$route.name
  }
}

window.$app = new Vue(vueOption)
window.$store = store
Vue.prototype.$store = store
Vue.prototype.useTableSearchBar = !!window.RPConfig.USE_TABLE_SEARCH

// TODO 监听iframe嵌入的子应用发生的消息，用于实现token失效同步问题（等待后端修改后联调）
window.addEventListener(
  'message',
  function(event) {
    if (event.data === 'ripples:sso:loginout') {
      window.$util.redirectToLogin()
    }
  },
  false
)
